<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateGoodsContentAPIRequest;
use App\Http\Requests\API\UpdateGoodsContentAPIRequest;
use App\Models\GoodsContent;
use App\Repositories\GoodsContentRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class GoodsContentController
 * @package App\Http\Controllers\API
 */

class GoodsContentAPIController extends AppBaseController
{
    /** @var  GoodsContentRepository */
    private $goodsContentRepository;

    public function __construct(GoodsContentRepository $goodsContentRepo)
    {
        $this->goodsContentRepository = $goodsContentRepo;
    }

    /**
     * Display a listing of the GoodsContent.
     * GET|HEAD /goodsContents
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->goodsContentRepository->pushCriteria(new RequestCriteria($request));
        $this->goodsContentRepository->pushCriteria(new LimitOffsetCriteria($request));
        $goodsContents = $this->goodsContentRepository->all();

        return $this->sendResponse($goodsContents->toArray(), 'Goods Contents retrieved successfully');
    }

    /**
     * Store a newly created GoodsContent in storage.
     * POST /goodsContents
     *
     * @param CreateGoodsContentAPIRequest $request
     *
     * @return Response
     */
    public function store(CreateGoodsContentAPIRequest $request)
    {
        $input = $request->all();

        $goodsContents = $this->goodsContentRepository->create($input);

        return $this->sendResponse($goodsContents->toArray(), 'Goods Content saved successfully');
    }

    /**
     * Display the specified GoodsContent.
     * GET|HEAD /goodsContents/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        /** @var GoodsContent $goodsContent */
        $goodsContent = $this->goodsContentRepository->findWithoutFail($id);

        if (empty($goodsContent)) {
            return $this->sendError('Goods Content not found');
        }

        return $this->sendResponse($goodsContent->toArray(), 'Goods Content retrieved successfully');
    }

    /**
     * Update the specified GoodsContent in storage.
     * PUT/PATCH /goodsContents/{id}
     *
     * @param  int $id
     * @param UpdateGoodsContentAPIRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateGoodsContentAPIRequest $request)
    {
        $input = $request->all();

        /** @var GoodsContent $goodsContent */
        $goodsContent = $this->goodsContentRepository->findWithoutFail($id);

        if (empty($goodsContent)) {
            return $this->sendError('Goods Content not found');
        }

        $goodsContent = $this->goodsContentRepository->update($input, $id);

        return $this->sendResponse($goodsContent->toArray(), 'GoodsContent updated successfully');
    }

    /**
     * Remove the specified GoodsContent from storage.
     * DELETE /goodsContents/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var GoodsContent $goodsContent */
        $goodsContent = $this->goodsContentRepository->findWithoutFail($id);

        if (empty($goodsContent)) {
            return $this->sendError('Goods Content not found');
        }

        $goodsContent->delete();

        return $this->sendResponse($id, 'Goods Content deleted successfully');
    }
}
